package demo5;
///上转型对象
/// 1. 直接赋值：子类对象赋值给父类对象
/// 2. 方法传参：形参为父类型引用，可以接收任意子类的对象

class Dw{
    int age;
    String name;

    public Dw(int age,String name){
        this.age = age;
        this.name = name;
    }
    public void bark(){
        System.out.println(name + "叫");
    }
}

class Bired extends Dw{
    public Bired(int age, String name){
        super(age,name);
    }

    public void bark(){
        System.out.println(name + "正在叽叽喳喳");
    }
}

public class Test1 {
    public static void main1(String[] args) {
        // 1. 直接赋值：子类对象赋值给父类对象
        Dw dw = new Bired(1,"bb");
        dw.bark();
    }

    //2. 方法传参：形参为父类型引用，可以接收任意子类的对象
    public static void fun(Dw dw){
        dw.bark();
    }
    public static void main2(String[] args) {
        Dw dw1 = new Bired(1,"gg");
        fun(dw1);
        Dw dw2 = new Bired(2,"zz");
        fun(dw2);
    }

    //3. 作返回值：返回任意子类对象
    public static Dw func2(){
        Bired bired = new Bired(1,"cc");
        return bired;
    }

    public static void main3(String[] args) {
        Dw dw = func2();
        dw.bark();
    }
}
